<?php



class openapi_api_function_v1_delivery extends openapi_api_function_abstract implements openapi_api_function_interface{

	public function add($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
		$obj_member = app::get('b2c')->model('members');
        if(!$obj_member->isAllowAddr($member_id)){
            return $this->apiData(false,'',400,app::get('b2c')->_('不能新增收货地址'));
        }
        $params['phone']['mobile'] = $params['mobile'];
        unset($params['mobile']);
        $params['phone']['telephone'] = $params['telephone'];
        unset($params['telephone']);
        $aData = $this->arrContentReplace($params);
        if($obj_member->insertRec($aData,$member_id,$message)){
            return $this->apiData(true,null,200,$message);
        }else{
            return $this->apiData(false,'',400,$message);
        }
	}

	public function getlist($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
		$obj_member_addrs = app::get('b2c')->model('member_addrs');
        $addrlist = $obj_member_addrs->getList('*',array('member_id'=>$member_id));
        if(count($addrlist)==0){
        	return $this->apiData(false,null,200,app::get('b2c')->_('还没有添加收货信息'));
        }
        foreach($addrlist as $k=>$v){
            $area = array();
            $area = explode(':',$v['area']);
            $addrlist[$k]['region_id'] = $area[2];
            $area = explode('/',$area[1]);
            
            if(in_array($area[0],array('北京','天津','上海','重庆'))){
                $area[0] = '';
            }
            $addrlist[$k]['area_str'] = implode("",$area);
        }
        return $this->apiData(true,array('addrlist'=>$addrlist),200);
	}

    
    public function detail($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $obj_member = app::get('b2c')->model('members');
        if($obj_member->check_addr($params['addr_id'],$member_id))
        {
            $aRet = $obj_member->getAddrById($params['addr_id']);
            $area = explode(':', $aRet['area']);
            $addr = explode('/', $area[1]);
            $code = $area[2];
            $arealist = array();
            $obj_regions_op = kernel::service('ectools_regions_apps', array('content_path'=>'ectools_regions_operation'));
            $regionId = 0;
            foreach ($addr as $key => $value) {
                $arealist[$value] = $obj_regions_op->getRegionById($regionId);
                foreach ($arealist[$value] as $key => $area) {
                    if($area['local_name']==$value){
                        $regionId = $area['region_id'];
                    }
                }
            }
            $aRet['arealist'] = $arealist;
            return $this->apiData(true,$aRet,200);
        }else{
            return $this->apiData(false,null,400,app::get('b2c')->_('地址不存在'));
        }
    }

    //by zhuhao
    public function modify($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $obj_member = app::get('b2c')->model('members');
        if($obj_member->check_addr($params['addr_id'],$member_id))
        {
            $params['phone']['mobile'] = $params['mobile'];
            unset($params['mobile']);
            $params['phone']['telephone'] = $params['telephone'];
            unset($params['telephone']);
            $aData = $this->arrContentReplace($params);
            if($obj_member->save_rec($aData,$member_id,$message)){
                return $this->apiData(true,null,200,app::get('b2c')->_('修改成功'));
            }
            else{
                 return $this->apiData(false,'',400,$message);
            }
        }else{
            return $this->apiData(false,'',400,app::get('b2c')->_('参数错误'));
        }

    }

    //by zhuhao
    public function delete($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $obj_member = app::get('b2c')->model('members');
        if(!$params['addr_id']) return $this->apiData(false,'',400,app::get('b2c')->_('参数错误'));
        if($obj_member->check_addr($params['addr_id'],$member_id))
        {
            if($obj_member->del_rec($params['addr_id'],$message,$member_id))
            {
                return $this->apiData(true,null,200,$message);
            }
            else
            {
                return $this->apiData(false,'',400,$message);
            }
        }
        else
        {
            return $this->apiData(false,'',400,$message);
        }

    }


	function arrContentReplace($array){
        if (is_array($array)){
            foreach($array as $key=>$v){
                $array[$key] = $this->arrContentReplace($array[$key]);
            }
        }
        else{
            $array = strip_tags($array);
        }
        return $array;
    }

}